# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=qt6-tools
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
_qtver=6.2.1
pkgver=${_qtver/-/}
pkgrel=2
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://www.qt.io'
license=(GPL3 LGPL3 FDL custom)
pkgdesc='A cross-platform application and UI framework (Development Tools, QtHelp) (mingw-w64)'
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-qt6-declarative"
             "${MINGW_PACKAGE_PREFIX}-qt6-activeqt"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-clang-tools-extra"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "rsync")
options=('!strip')
_pkgfn="${_realname/6-/}-everywhere-src-${_qtver}"
source=("https://download.qt.io/official_releases/qt/${pkgver%.*}/$_qtver/submodules/$_pkgfn.tar.xz"
        001-appending-qt6-to-remove-qt5-conflict.patch
        002-qt6-windeployqt-fixes.patch)
sha256sums=('5a856d3d3d5fe6e15dc3f1af707a0ef1df2e687850403fc94af635edb9312bfb'
            '0b3fccd70feb0a5787a1711ceb5aa87eaf7e995c3fa43ecb53638b069cf32e75'
            '59837bddb18965fc2994f4321f431101b3e7775b975bd1ac568e50d6d4cc4e3c')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd $srcdir/${_pkgfn}

  apply_patch_with_msg \
    001-appending-qt6-to-remove-qt5-conflict.patch \
    002-qt6-windeployqt-fixes.patch
}

build() {
  cd ${srcdir}
  [[ -d build-${MSYSTEM} ]] && rm -rf build-${MSYSTEM}
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  local -a _clangcpp
  if [[ "${MINGW_PACKAGE_PREFIX}" == *-clang-* ]]; then
    _clangcpp="ON"
  else
    _clangcpp="OFF"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  cmake \
    -GNinja \
    -DCMAKE_BUILD_TYPE=RelWithDebInfo \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DQT_FEATURE_clang=ON \
    -DQT_FEATURE_clangcpp=${_clangcpp} \
    ../${_pkgfn}

  cmake --build .
}

package_qt6-tools() {
  depends=("${MINGW_PACKAGE_PREFIX}-qt6-base"
           $([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-clang" ) )
  optdepends=("${MINGW_PACKAGE_PREFIX}-qt6-declarative"
              "${MINGW_PACKAGE_PREFIX}-qt6-activeqt"
              "${MINGW_PACKAGE_PREFIX}-clang: for qdoc-qt6")
  groups=("${MINGW_PACKAGE_PREFIX}-qt6")

  DESTDIR=${pkgdir} cmake --install build-${MSYSTEM}

  # Restore qtdiag6
  ln -s ${pkgdir}${MINGW_PREFIX}/bin/qtdiag-qt6 ${pkgdir}${MINGW_PREFIX}/bin/qtdiag6.exe

  # Seperate debug-info files
  rsync -armR --remove-source-files --include="*/" --include="*.debug" --exclude="*" --prune-empty-dirs ${pkgdir}/.${MINGW_PREFIX} ${srcdir}/${MSYSTEM}-debug/

  install -d "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
  install -Dm644 $_pkgfn/LICENSE* -t "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
}

package_qt6-tools-debug() {
  depends=("${MINGW_PACKAGE_PREFIX}-qt6-base-debug"
           "${MINGW_PACKAGE_PREFIX}-${_realname}")
  groups=("${MINGW_PACKAGE_PREFIX}-qt6-debug")

  cp -rf ${srcdir}/${MSYSTEM}-debug${MINGW_PREFIX} "${pkgdir}"/
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
